<?php
namespace Home\Server;

/**
 * Created by PhpStorm.
 * User: Z
 * Date: 2017/3/8
 * Time: 13:17
 * 用户加密、解密TOKEN
 */
class LoginEncryptService
{
    private $user_info;
    private $nowTime;
    private $tokenKEY = 'xmks_USER_KEY';

    function __construct()
    {
        $this ->nowTime = time();
    }

    /**
     * Function User_LoginEncode
     * @param $uid
     * @param $password   用户UID
     * @return string     用户数据库中记录的密码
     *
     * Author: 黄盖
     * Date: 2017-3-8 13:26
     * Info: 登录用户之后token加密
     */
    public function User_LoginEncode( $uid, $password = '' )
    {
        //获取UID的密码
        $password = ! empty( $password ) ? $password : $this ->getUserPWD( $uid );

        if( $password == false )
        {
            return false;
        }

        //进行加密
        return $this ->enCode( $this ->Generate_Sixteen( $uid ), $this ->nowTime, $this ->LoginPassWordEncode( $password ) );
    }

    /**
     * LoginEncryptService::User_LoginDecode()
     * 解密用户登录token
     * @return void
     */
    public function User_LoginDecode( $token )
    {
        if( strlen( $token ) > 42 )
        {
            //验证
            return $this ->LoginDecode( $token );
        }
        else
        {
            return false;
        }
    }

    /**
     * LoginEncryptService::LoginDecode()
     * 解密
     * @param mixed $Login_Token
     * @return void
     */
    public function LoginDecode( $Login_Token )
    {
        //登录后的Token
        $token = substr( $Login_Token, -32 );

        //加密时的时间
        $_time = substr( $Login_Token, -42, 10 );

        //16进制的UID
        $sixteen_uid = substr( $Login_Token, 0, -42 );

        //解密后正常的UID
        $uid = intval( $this ->Cracking_Sixteen( $sixteen_uid ) );

        //获取UID的密码
        $password = $this ->getUserPWD( $uid );

        if( $password == false )
        {
            return false;
        }

        //正向加密, 用当前用户新的密码, 加密之后, 是否与Token吻合
        $encode = $this ->enCode( $sixteen_uid, $_time, $this ->LoginPassWordEncode( $password ) );

        if( $Login_Token == $encode )
        {
            return $this ->user_info;
        }
        else
        {
            return false;
        }
    }

    /**
     * LoginEncryptService::enCode()
     * 加密token
     * @param mixed $sixteen_uid    用户uid 的 16 进制
     * @param mixed $time           加密时的时间戳
     * @param mixed $pwd            md5( 用户数据库密码 + KEY )
     * @return
     */
    private function enCode( $sixteen_uid, $time, $pwd )
    {
        return $sixteen_uid . $time. md5( $sixteen_uid . $time . $pwd );
    }

    /**
     * LoginEncryptService::getUserPWD()
     * 获取用户密码
     * @param mixed $uid
     * @return
     */
    private function getUserPWD( $uid )
    {
        $this ->user_info = ( new \Home\Model\UserModel() ) ->getUserInfo( $uid );

        return isset( $this ->user_info['f_user_pwd'] ) ? $this ->user_info['f_user_pwd'] : false;
    }

    /**
     * LoginEncryptService::Generate_Sixteen()
     * 10进制 转16进制
     *      伪装原UID  20170308
     * @return
     */
    public function Generate_Sixteen( $uid )
    {
        return dechex( $uid + 20170308 );
    }

    /**
     * LoginEncryptService::Cracking_Sixteen()
     * 16进制 转 10进制
     * @return
     */
    public function Cracking_Sixteen( $uid )
    {
        return hexdec( $uid ) - 20170308;
    }

    /**
     * LoginEncryptService::LoginPassWordEncode()
     * 用户的密码加密
     * @param mixed $userPassword  用户输入的密码
     * @return  加密后的密码
     */
    public function LoginPassWordEncode( $userPassword )
    {
        return md5( $userPassword . $this ->tokenKEY );
    }
}